package com.molyfun.parents.activity.report;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.rest.bean.RepoetFromsLineListResponse;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 课程
 */
public class ReportFromsCourseAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_HEADER = 1001;
    private static final int TYPE_CONTENT = 1002;
    private static final int TYPE_FOTTER = 1003;
    private Context mContext;
    private boolean isSlector = true;
    private int mOldChoosePosition = -1;
    private int mSelectedPosition = -1;


    private ArrayList<RepoetFromsLineListResponse.LineListBean.CoursenamelistBean> mDataList;


    public ReportFromsCourseAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void initData(ArrayList<RepoetFromsLineListResponse.LineListBean.CoursenamelistBean> resultlist) {
        this.mDataList = resultlist;

        int position = 0;
        for (RepoetFromsLineListResponse.LineListBean.CoursenamelistBean courseBean : resultlist) {
            if (courseBean.isSelector()) {
                mOldChoosePosition = position;
                mSelectedPosition = mOldChoosePosition;
            }
            position++;
        }

        notifyDataSetChanged();
    }

    public RepoetFromsLineListResponse.LineListBean.CoursenamelistBean getChoosedCourse() {
        if (mOldChoosePosition == mSelectedPosition) {
//                return null;
        }
        return mDataList.get(mSelectedPosition);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ReportFromsCourseViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_sigle_textview_item, parent, false));

    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final RepoetFromsLineListResponse.LineListBean.CoursenamelistBean coursenamelistBean = mDataList.get(position);
        final ReportFromsCourseViewHolder mCourseViewHolder = (ReportFromsCourseViewHolder) holder;
        mCourseViewHolder.mTextView.setText(coursenamelistBean.getName());

        mCourseViewHolder.mLine.setVisibility(View.GONE);
        if (mSelectedPosition == position) {
            coursenamelistBean.setSelector(true);
            mCourseViewHolder.mTextView.setTextColor(mContext.getResources().getColor(R.color.color_white));
            mCourseViewHolder.mTextView.setBackgroundColor(mContext.getResources().getColor(R.color.course_choose_right_item_click));
        } else {
            coursenamelistBean.setSelector(false);
            mCourseViewHolder.mTextView.setTextColor(mContext.getResources().getColor(R.color.color_title_text));
            mCourseViewHolder.mTextView.setBackgroundColor(mContext.getResources().getColor(R.color.color_white));
        }
        mCourseViewHolder.mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSelectedPosition = position;
                if (reportFromsCourseAdapterCallback != null) {
                    reportFromsCourseAdapterCallback.setTagertId(coursenamelistBean);
                }
                notifyDataSetChanged();
            }
        });

    }

    @Override
    public int getItemCount() {
        if (mDataList == null || mDataList.size() == 0) {
            return 0;
        }
        return mDataList.size();
    }

    class ReportFromsCourseViewHolder extends RecyclerView.ViewHolder {


        @InjectView(R.id.line)
        public View mLine;
        @InjectView(R.id.textView_item_course)
        public TextView mTextView;

        public ReportFromsCourseViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    public void setReportFromsCourseAdapterListener(ReportFromsCourseAdapterCallback reportFromsCourseAdapterCallback) {
        this.reportFromsCourseAdapterCallback = reportFromsCourseAdapterCallback;
    }

    private ReportFromsCourseAdapterCallback reportFromsCourseAdapterCallback;

    public interface ReportFromsCourseAdapterCallback {
        void setTagertId(RepoetFromsLineListResponse.LineListBean.CoursenamelistBean coursenamelistBean);
    }
}
